package com.superpai.view.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.TextView;

import com.superpai.R;
import com.superpai.domain.Item;
import com.superpai.utils.BitmapUtils;
import com.superpai.view.download.ILoadImageListener;
import com.superpai.view.download.ImageLoadAdapter;

/**
 * @author Devin.Hu
 * @date 2011-8-23
 * @version V1.0
 * @description 产品页adapter
 */
public class ProductsAdapter extends ImageLoadAdapter implements ILoadImageListener {

	private Context mContext;
	private LayoutInflater mInflater;
	private List<Item> mList;
	private TextView product_item_price;
	private ImageView product_item_image;
	private int mTotalCount;


	public ProductsAdapter(Context context) {
		super(context);
		mContext = context;
		mInflater = LayoutInflater.from(mContext);
		this.setLoadImageListener(this);
	}

	public List<Item> getmList() {
		return mList;
	}

	public void setmList(List<Item> list) {
		mTotalCount += list.size();
		if(null == mList){
			mList = list;
		}else{
			mList.addAll(list);
		}
		setDataCount(mList.size());
	}

	public int getmTotalCount() {
		return mTotalCount;
	}
	
	public boolean isEmpty(){
		return mList==null || mList.isEmpty() ? true : false;
	}
	
	public Item getItemByPosition(int position){
		return mList==null ? null : mList.get(position);
	}
	
	@Override
	public View getCacheDefaultView(int position) {
		View view = mInflater.inflate(R.layout.products_item, null);
		product_item_price = (TextView)view.findViewById(R.id.product_item_price);
		product_item_image = (ImageView)view.findViewById(R.id.product_item_image);
		product_item_price.setText("￥"+mList.get(position).getItemPrice());
		product_item_image.setTag(mList.get(position).getPicLink());
		return view;
	}

	@Override
	public void processDefaultState(View view, int position) {
		ImageView imageView = (ImageView)view.findViewById(R.id.product_item_image);
		imageView.setImageResource(R.drawable.home_gallery_bg);
	}

	@Override
	public String getDownloadUrl(int position) {
		return mList.get(position).getPicLink();
	}

	@Override
	public void processEmptyState(View view, int position) {
		ImageView imageView = (ImageView)view.findViewById(R.id.product_item_image);
		imageView.setImageResource(R.drawable.home_gallery_bg);
	}

	@Override
	public void processErrorState(View view, int position) {
		ImageView imageView = (ImageView)view.findViewById(R.id.product_item_image);
		imageView.setImageResource(R.drawable.home_gallery_bg);
	}

	@Override
	public Bitmap processDownloadBytes(View view, byte[] bytes) {
		int width = view.findViewById(R.id.product_item_image).getWidth();
		
		Bitmap bmp = BitmapUtils.getCorrectBmp(bytes, 0, Config.RGB_565);
		if(bmp!=null){
			float scale = (float)width/bmp.getWidth();
			Bitmap cornerBmp = BitmapUtils.getRoundedCornerBitmap(bmp, scale, 7, Config.ARGB_8888);
			bmp.recycle();
			return cornerBmp;
		}
		return null;
	}

	@Override
	public void processDownloadBmp(View view, Bitmap bmp, int position) {
		ImageView imageView = (ImageView)view.findViewById(R.id.product_item_image);
		imageView.setImageBitmap(bmp);
		imageView.setScaleType(ScaleType.CENTER_INSIDE);
		view.invalidate();
	}
}
